#!/usr/bin/env python
# -*- coding:utf-8 -*-

from ConfigParser import ConfigParser
from engine import singleton
from dm_py_util import (DEBUG_LOG,
                        ERROR_LOG,
                        WARN_LOG,
                        PY_Redis_Cache)
from xpinyin import Pinyin
from tornado import escape
import json
import re
import jieba
import string
import checkstr_legitimacy
import hashlib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

pattern = re.compile(ur'([\u4e00-\u9fa5a-zA-Z0-9]+)')
numPattern = re.compile(ur'([0-9]+)')


@singleton
class inkeReplaceAdsManager(object):

    def __init__(self):
        pass

    def init(self, config):
        try:
            cfg = ConfigParser()
            cfg.read(config)
        except Exception as e:
            ERROR_LOG('init AdsManager failed', e)
            exit(-1)
        try:
            self._nick_censor_word = {}
            self._desc_censor_word = {}
            self._content_censor_word = {}
            nick_censor_word_filename = cfg.get(
                'AdsManager', 'nick_censorword')
            desc_censor_word_filename = cfg.get(
                'AdsManager', 'desc_censorword')
            content_censor_word_filename = cfg.get(
                'AdsManager', 'content_censorword')
            self._init_censor_word(
                nick_censor_word_filename, desc_censor_word_filename, content_censor_word_filename)
            self.pinyin = Pinyin()

            repeat_cache_conf = cfg.get('Cache', 'conf')
            self.repeat_cache = PY_Redis_Cache()
            self.repeat_cache.init(repeat_cache_conf)

            self.repeat_cache_key = cfg.get('CacheKey', 'value')
            self.repeat_num = cfg.get('RepeatNum', 'value')
            self.repeat_percent = cfg.get('RepeatPercent', 'value')
            self.message_expire_time = int(
                cfg.get('MessageExpireTime', 'value'))
            self.message_default_len = cfg.get('MessageDefaultLen', 'value')

        except Exception as e:
            ERROR_LOG('init censorWord failed', e)
            exit(-1)
        try:
            slice_word_filename = cfg.get('AdsManager', 'sliceword')
            jieba.load_userdict(slice_word_filename)
        except Exception as e:
            ERROR_LOG('init sliceWord failed', e)
            exit(-1)

    def _init_censor_word(self, filename_nick, filename_desc, filename_content):
        with open(filename_nick, 'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                if not self._nick_censor_word.get(str(line).strip(), None):
                    self._nick_censor_word[
                        str(line).strip().decode('utf-8')] = True
        with open(filename_desc, 'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                if not self._desc_censor_word.get(str(line).strip(), None):
                    self._desc_censor_word[
                        str(line).strip().decode('utf-8')] = True
        with open(filename_content, 'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                if not self._content_censor_word.get(str(line).strip(), None):
                    self._content_censor_word[
                        str(line).strip().decode('utf-8')] = True

    def _character_filter(self, source_str):
        if not source_str or len(source_str) == 0:
            return None
        match = pattern.findall(str(source_str).decode('utf-8'))
        if not match:
            return None
        return ''.join(match)

    def _find(self, source, word, check):
        replace = []
        if not word or len(word) == 0:
            return None
        sentence = self._character_filter(word)
        if not sentence or len(sentence) == 0:
            return None
        slice_word = jieba.lcut(sentence)
        word_sort = sorted(slice_word)
        for s_word in word_sort:
            if check == True:
                if len(s_word) >= 6:
                    match = numPattern.findall(s_word)
                    if not match:
                        continue
                    try:
                        if string.atoi(''.join(match)) > 100000:
                            replace.append(''.join(match))
                    except Exception:
                        pass
            if source == 'nick':
                if self._nick_censor_word.get(s_word, False):
                    replace.append(s_word)
            elif source == 'description':
                if self._desc_censor_word.get(s_word, False):
                    replace.append(s_word)
            elif source == 'content':
                # print self._desc_censor_word
                if self._content_censor_word.get(s_word, False):
                    replace.append(s_word)
        return replace

    def checkcontent(self, source, word):
        if source == 'content':
            check = True
        else:
            check = False
        return checkstr_legitimacy.check_str(self.pinyin, word)

    def check_repeat_content(self, msg):

        # 0 代表不是, 1代表是
        res = 1
        key = self._get_repeat_content_cache_key(msg)
        # if isinstance(msg, str):
        #     try:
        #         msg = escape.to_unicode(msg)
        #     except Exception as e:
        #         ERROR_LOG('check_repeat_message error', e)
        #         return 0
        # new_msg = self._character_filter(msg)
        # if len(new_msg) <= int(self.message_default_len):
        #     DEBUG_LOG('check_repeat_message new_msg_len', len(new_msg))
        #     return 0

        ret = self._get_repeat_content_from_cache(key)

        DEBUG_LOG('check_repeat_content, ret', ret)
        if ret:
            self._set_repeat_message_expire_time_to_cache(key)
            res = 1
        else:
            self._set_repeat_content_to_cache(key)
            res = 0
        return res

    def check(self, source, word):
        if source == 'nick':
            check = True
        else:
            check = False

        replace = self._find(source, word, check)
        if not replace or len(replace) == 0:
            return False
        else:
            return True

    def check_Sensitiveword_combination(self, source, word):
        replace = self._find(source, word, False)
        DEBUG_LOG('replace num:', len(replace))
        if not replace or len(replace) <= 3:
            return False
        else:
            return True

    def replace(self, source, word):
        if source == 'nick':
            check = True
        else:
            check = False
        replace = self._find(source, word, check)

        word = word.decode('utf-8')
        if not replace or len(replace) == 0:
            return word

        for b_word in replace:
            part_str = len(b_word) * '*'
            word = word.replace(b_word, part_str)
        return word

    def _convert_pinyin(self, word):
        try:
            word = self.pinyin.get_pinyin(
                escape.to_unicode(word), '', False, True).encode('utf-8')
        except Exception as e:
            ERROR_LOG('_convert_pinyin error', e)
            return None
        return word

    def check_repeat_message(self, send_id, recv_id, msg):

        # 0 代表不是, 1代表是
        key = self._get_repeat_message_cache_key(send_id, recv_id)
        if isinstance(msg, str):
            try:
                msg = escape.to_unicode(msg)
            except Exception as e:
                ERROR_LOG('check_repeat_message error', e)
                return 0
        new_msg = self._character_filter(msg)
        if len(new_msg) <= int(self.message_default_len):
            DEBUG_LOG('check_repeat_message new_msg_len', len(new_msg))
            return 0

        ret = self._get_repeat_message_from_cache(key)
        num = 1

        DEBUG_LOG('check_repeat_message, ret', ret)
        if ret:
            old_msg = ret['msg']
            num = ret['num']

            if self._check_repeat_message_percent(old_msg, new_msg):
                num += 1
                if num > int(self.repeat_num):
                    return 1
        if not ret:
            self._set_repeat_message_expire_time_to_cache(key)

        self._set_repeat_message_to_cache(key, new_msg, num)
        return 0

    def _get_repeat_message_from_cache(self, key):
        try:
            ret = self.repeat_cache.get_from_cache(key)
        except Exception as e:
            ERROR_LOG('_get_repeat_message_from_cache error', e)
            return None
        if not ret:
            return None

        val = json.loads(ret)

        DEBUG_LOG('_get_repeat_message_from_cache, val', val)

        return val

    def _get_repeat_content_from_cache(self, key):
        try:
            ret = self.repeat_cache.get_from_cache(key)
        except Exception as e:
            ERROR_LOG('_get_repeat_content_from_cache error', e)
            return None

        DEBUG_LOG('_get_repeat_content_from_cache, ret', ret)

        return ret

    def _set_repeat_message_to_cache(self, key, msg, num):
        message = {}
        message['msg'] = msg
        message['num'] = num
        val = json.dumps(message)
        DEBUG_LOG('_set_repeat_message_to_cache, val', val)

        try:
            self.repeat_cache.put_to_cache(key, val)
        except Exception as e:
            ERROR_LOG('_set_repeat_message_to_cache error', e)

    def _set_repeat_content_to_cache(self, key, msg='', num=1):
        try:
            self.repeat_cache.put_to_cache_ex(
                key, '1', self.message_expire_time)
        except Exception as e:
            ERROR_LOG('_set_repeat_content_to_cache error', e)

    def _set_repeat_message_expire_time_to_cache(self, key):
        try:
            self.repeat_cache.set_key_expire(
                key, self.message_expire_time)
        except Exception as e:
            ERROR_LOG('_set_content_expire_time_to_cache error', e)

    def _del_repeat_message_from_cache(self, key):
        try:
            self.repeat_cache.del_from_cache(key)
        except Exception as e:
            ERROR_LOG('_del_repeat_message_from_cache error', e)

    def _check_repeat_message_percent(self, old_msg, new_msg):
        old_count = len(old_msg)
        new_count = len(new_msg)
        repeat_count = 0

        for word in new_msg:
            if word in old_msg:
                repeat_count += 1

        DEBUG_LOG('_check_repeat_message_percent, old_count=%s, new_count=%s, repeat_count=%s' % (
            old_count, new_count, repeat_count))
        if old_count > new_count:
            repeat_percent = float(repeat_count) / float(old_count)
        else:
            repeat_percent = float(repeat_count) / float(new_count)

        DEBUG_LOG('_check_repeat_message_percent, repeat_percent', repeat_percent)

        if int(repeat_percent * 100) > int(self.repeat_percent):
            return True
        return False

    def _get_repeat_message_cache_key(self, send_id, recv_id):
        return self.repeat_cache_key + str(send_id) + str(recv_id)

    def _get_repeat_content_cache_key(self, msg):
        md5 = hashlib.md5()
        md5.update(msg)
        key = md5.hexdigest()
        DEBUG_LOG('ChatMsg _get_rechat_content_cache_key key', key)

        return key


if __name__ == '__main__':
    manager = inkeReplaceAdsManager()
    # manager.init('./ads_manager.ini')
    manager.init('/a8root/conf/inke_replace_ads/ads_manager.ini')
    # str1 = '我又没有最好①②'
    # str1 = '💎'
    # str1 = '💰'
    # str1 = '加💍'
    # str1 = '我要好好\t \n学习天天向上1234你好,   啊我不知道怎么办才好啊'
    # str1 = manager._character_filter(str1)
    # print type(str1)
    # print len(str1)

    # f = 0.222
    # f = f * 100
    # print int(f)

    # for word in str1:
    #     print word

    # if '我' in str1:
    #     print 'ok', '我'
    # if '爱' in str1:
    #     print 'ok', '爱'
    # if '北' in str1:
    #     print 'ok', '北'
    # if '京' in str1:
    #     print 'ok', '京'
    # if '天' in str1:
    #     print 'ok', '天'
    # if '安' in str1:
    #     print 'ok', '安'
    # if '4' in str1:
    #     print 'ok', '4'
    # if '5' in str1:
    #     print 'ok', '5'
    # if '6' in str1:
    #     print 'ok', '6'
    # if '7' in str1:
    #     print 'ok', '7'

    # seg_list = jieba.cut(str1, cut_all=True)
    # for seg in seg_list:
    #     print seg

    # str1 = escape.utf8(str1)
    # print type(str1)
    # print len(str1)

    print manager.check_Sensitiveword_combination('content', '淘宝 刷单 工作 客服 酬劳')
    # print manager.replace('nick', str1)
    # print manager.replace('nick', '1千钻十元Q1203557')
    # print manager.replace('nick', '30员5万鉆石+Q656961')
    # print manager.replace('nick', '认证V看签名+不需要17级')
    # print manager.replace('nick', '认证不用17级加我vx11637489六9')
    # print manager.replace('nick', '10沅=2000鉆需要+签名Q')
    # print manager.replace('nick', '╋Q❺❸❼！先货丶╋Q❺❸❼！先货丶')
    # print manager.replace('nick', '我爱北京天安门123')
    # print manager.replace('nick', '+q１２３４123')
    # print manager.replace('desc', '微博：白静cherry.百度：混血白静。'.decode('utf-8'))
    # print manager.replace('desc', '💍')
    # print manager.replace('desc', '💍')
    # print manager.replace('desc', '💎')
